package org.yaac.shared.blob;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.util.Date;

import com.google.gwt.view.client.ProvidesKey;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 * 
 */
public class BlobInfoDTO implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final ProvidesKey<BlobInfoDTO> KEY_PROVIDER = new ProvidesKey<BlobInfoDTO>() {
		@Override
		public Object getKey(BlobInfoDTO item) {
			return item == null ? null : item.getBlobKeyStr();
		}
	};
	
	private String blobKeyStr;

	private Date creation;

	private String contentType;

	private String filename;
	
	private Long size;
	
	private String md5Hash;

	/**
	 * used by GWT
	 */
	@SuppressWarnings("unused")
	private BlobInfoDTO(){}
	
	public BlobInfoDTO(String blobKeyStr, Date creation, String contentType, String filename, Long size, String md5Hash) {
		super();
		
		this.blobKeyStr = checkNotNull(blobKeyStr);
		this.creation = checkNotNull(creation);
		this.contentType = checkNotNull(contentType);
		this.filename = checkNotNull(filename);
		this.size = checkNotNull(size);
		this.md5Hash = md5Hash == null ? "" : md5Hash;
	}

	public String getBlobKeyStr() {
		return blobKeyStr;
	}

	public Date getCreation() {
		return creation;
	}

	public String getContentType() {
		return contentType;
	}

	public String getFilename() {
		return filename;
	}

	public Long getSize() {
		return size;
	}

	public String getMd5Hash() {
		return md5Hash;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((blobKeyStr == null) ? 0 : blobKeyStr.hashCode());
		result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
		result = prime * result + ((creation == null) ? 0 : creation.hashCode());
		result = prime * result + ((filename == null) ? 0 : filename.hashCode());
		result = prime * result + ((md5Hash == null) ? 0 : md5Hash.hashCode());
		result = prime * result + ((size == null) ? 0 : size.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BlobInfoDTO other = (BlobInfoDTO) obj;
		if (blobKeyStr == null) {
			if (other.blobKeyStr != null)
				return false;
		} else if (!blobKeyStr.equals(other.blobKeyStr))
			return false;
		if (contentType == null) {
			if (other.contentType != null)
				return false;
		} else if (!contentType.equals(other.contentType))
			return false;
		if (creation == null) {
			if (other.creation != null)
				return false;
		} else if (!creation.equals(other.creation))
			return false;
		if (filename == null) {
			if (other.filename != null)
				return false;
		} else if (!filename.equals(other.filename))
			return false;
		if (md5Hash == null) {
			if (other.md5Hash != null)
				return false;
		} else if (!md5Hash.equals(other.md5Hash))
			return false;
		if (size == null) {
			if (other.size != null)
				return false;
		} else if (!size.equals(other.size))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "BlobInfoDTO [blobKeyStr=" + blobKeyStr + ", creation=" + creation + ", contentType=" + contentType
				+ ", filename=" + filename + ", size=" + size + ", md5Hash=" + md5Hash + "]";
	}
}
